<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>服务配置管理 - FuYu Services</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
            color: white;
            padding: 30px;
            text-align: center;
            position: relative;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 0 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .stats {
            position: absolute;
            top: 20px;
            right: 30px;
            background: rgba(255,255,255,0.1);
            padding: 10px 20px;
            border-radius: 25px;
            backdrop-filter: blur(10px);
        }

        .controls {
            padding: 20px 30px;
            background: #f8f9fa;
            border-bottom: 1px solid #e9ecef;
            display: flex;
            gap: 20px;
            align-items: center;
            flex-wrap: wrap;
        }

        .search-box {
            flex: 1;
            min-width: 300px;
            position: relative;
        }

        .search-box input {
            width: 100%;
            padding: 12px 20px 12px 50px;
            border: 2px solid #ddd;
            border-radius: 25px;
            font-size: 16px;
            transition: all 0.3s ease;
        }

        .search-box input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .search-box::before {
            content: "🔍";
            position: absolute;
            left: 18px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 18px;
        }

        .filter-tabs {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }

        .tab {
            padding: 8px 16px;
            background: white;
            border: 2px solid #ddd;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 14px;
            white-space: nowrap;
        }

        .tab.active {
            background: #667eea;
            color: white;
            border-color: #667eea;
        }

        .tab:hover {
            border-color: #667eea;
        }

        .content {
            padding: 30px;
        }

        .category-section {
            margin-bottom: 40px;
        }

        .category-header {
            display: flex;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #eee;
        }

        .category-header h2 {
            font-size: 1.5rem;
            color: #2c3e50;
            margin-right: 15px;
        }

        .category-count {
            background: #667eea;
            color: white;
            padding: 4px 12px;
            border-radius: 15px;
            font-size: 0.9rem;
            font-weight: bold;
        }

        .services-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
            gap: 20px;
        }

        .service-card {
            background: white;
            border: 1px solid #e9ecef;
            border-radius: 12px;
            padding: 20px;
            transition: all 0.3s ease;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .service-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            border-color: #667eea;
        }

        .service-header {
            display: flex;
            justify-content: between;
            align-items: flex-start;
            margin-bottom: 15px;
        }

        .service-title {
            font-size: 1.2rem;
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 5px;
            line-height: 1.3;
        }

        .service-filename {
            font-size: 0.9rem;
            color: #6c757d;
            font-family: 'Courier New', monospace;
            background: #f8f9fa;
            padding: 2px 8px;
            border-radius: 4px;
        }

        .service-stats {
            display: flex;
            gap: 15px;
            margin: 10px 0;
            font-size: 0.85rem;
            color: #6c757d;
        }

        .service-stat {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .service-commands {
            margin-top: 15px;
        }

        .command-tag {
            display: inline-block;
            background: #e9ecef;
            color: #495057;
            padding: 2px 8px;
            border-radius: 12px;
            font-size: 0.8rem;
            margin: 2px 4px 2px 0;
        }

        .service-actions {
            position: absolute;
            top: 15px;
            right: 15px;
            display: flex;
            gap: 5px;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .service-card:hover .service-actions {
            opacity: 1;
        }

        .action-btn {
            width: 30px;
            height: 30px;
            border: none;
            border-radius: 50%;
            background: #667eea;
            color: white;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        .action-btn:hover {
            background: #5a67d8;
            transform: scale(1.1);
        }

        .hidden {
            display: none;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #6c757d;
        }

        .empty-state h3 {
            font-size: 1.5rem;
            margin-bottom: 10px;
        }

        .loading {
            text-align: center;
            padding: 40px;
            font-size: 1.2rem;
            color: #6c757d;
        }

        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.8);
            z-index: 1000;
        }

        .modal-content {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: white;
            width: 90%;
            max-width: 1200px;
            height: 90%;
            border-radius: 12px;
            display: flex;
            flex-direction: column;
        }

        .modal-header {
            padding: 20px;
            background: #2c3e50;
            color: white;
            border-radius: 12px 12px 0 0;
            display: flex;
            justify-content: between;
            align-items: center;
        }

        .modal-body {
            flex: 1;
            overflow: hidden;
        }

        .close-btn {
            background: none;
            border: none;
            color: white;
            font-size: 24px;
            cursor: pointer;
            padding: 5px;
        }

        #workflowEditor {
            width: 100%;
            height: 100%;
            border: none;
        }

        @media (max-width: 768px) {
            .header h1 {
                font-size: 2rem;
            }
            
            .stats {
                position: static;
                margin-top: 20px;
                text-align: center;
            }
            
            .controls {
                flex-direction: column;
                align-items: stretch;
            }
            
            .filter-tabs {
                justify-content: center;
            }
            
            .services-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div class="stats">
                <div>📊 <span id="totalServices">336</span> 个服务</div>
            </div>
            <h1>FuYu 服务配置管理</h1>
            <p>管理和配置工作流服务 - 可视化编辑器集成</p>
        </div>

        <div class="controls">
            <div class="search-box">
                <input type="text" id="searchInput" placeholder="搜索服务名称、文件名或命令...">
            </div>
            <div class="filter-tabs">
                <div class="tab active" data-category="all">全部</div>
                <div class="tab" data-category="audio">音频 (6)</div>
                <div class="tab" data-category="alarm">闹钟 (3)</div>
                <div class="tab" data-category="voice">语音 (2)</div>
                <div class="tab" data-category="text">文本 (2)</div>
                <div class="tab" data-category="speech">语音识别 (2)</div>
                <div class="tab" data-category="other">其他</div>
            </div>
            <div style="margin-left: auto;">
                <button class="tab" onclick="window.open('index.html', '_blank')" style="background: #28a745; color: white; border-color: #28a745;">
                    🚀 打开编辑器
                </button>
                <button class="tab" onclick="window.open('service-scanner.html', '_blank')" style="background: #ffc107; color: black; border-color: #ffc107; margin-left: 10px;">
                    🔍 扫描服务
                </button>
            </div>
        </div>

        <div class="content">
            <div class="loading" id="loading">正在加载服务配置...</div>
            
            <div id="servicesContainer" style="display: none;">
                <!-- 服务分类将在这里动态生成 -->
            </div>

            <div class="empty-state hidden" id="emptyState">
                <h3>🔍 没有找到匹配的服务</h3>
                <p>尝试调整搜索条件或选择不同的分类</p>
            </div>
        </div>
    </div>

    <!-- 工作流编辑器模态框 -->
    <div class="modal" id="editorModal">
        <div class="modal-content">
            <div class="modal-header">
                <h3 id="modalTitle">工作流编辑器</h3>
                <button class="close-btn" onclick="closeEditor()">&times;</button>
            </div>
            <div class="modal-body">
                <iframe id="workflowEditor" src="index.html"></iframe>
            </div>
        </div>
    </div>

    <script>
        // 服务配置数据
        const servicesData = [
            // 音频服务
            { name: "音频点读", filename: "audio-point-read.json", category: "audio", commands: ["AsrStream", "DeviceIntentionVoice", "skill-play-text"], nodes: 15 },
            { name: "音频点读英语", filename: "audio-point-read-english.json", category: "audio", commands: ["AsrStream", "DeviceIntentionVoice", "skill-play-text"], nodes: 12 },
            { name: "音频监听", filename: "audio-listen.json", category: "audio", commands: ["AsrStream", "AudioListen"], nodes: 8 },
            { name: "英语音频监听", filename: "audio-listen-english.json", category: "audio", commands: ["AsrStream", "AudioListen"], nodes: 10 },
            { name: "音频OSS", filename: "audio-oss.json", category: "audio", commands: ["TencentPostObject", "UrlOutput"], nodes: 6 },
            { name: "DCS聊天测试", filename: "audio-dcschat-test.json", category: "audio", commands: ["DcsChat", "AsrStream"], nodes: 9 },
            
            // 闹钟服务
            { name: "闹钟设置流程", filename: "alarm-clock-set.json", category: "alarm", commands: ["DeviceIntentionVoice", "AsrStream", "skill-alarm-clock-set"], nodes: 6 },
            { name: "闹钟关闭", filename: "alarm-clock-close.json", category: "alarm", commands: ["skill-alarm-clock-close"], nodes: 4 },
            { name: "闹钟删除", filename: "alarm-clock-delete.json", category: "alarm", commands: ["DeviceIntentionVoice", "skill-alarm-clock-delete"], nodes: 5 },
            
            // 语音服务
            { name: "语音呼叫流程", filename: "voice-call.json", category: "voice", commands: ["DeviceIntentionVoice", "PreVoiceCall", "Condition", "VoiceCall"], nodes: 18 },
            { name: "语音克隆音色流程", filename: "voice-clone.json", category: "voice", commands: ["BodyToJson", "CloneVoice", "TencentPostObject"], nodes: 7 },
            
            // 文本服务
            { name: "文本合成语音", filename: "text-audio.json", category: "text", commands: ["GetUniqueKEY", "BodyToJson", "TtsStream", "TencentPostObject"], nodes: 8 },
            { name: "文本优化器流程", filename: "text-optimizer.json", category: "text", commands: ["BodyToJson", "AiRoleChat", "TextOptimizer"], nodes: 7 },
            
            // 语音识别服务
            { name: "语音转文本流程", filename: "speech-to-text.json", category: "speech", commands: ["BodyToJson", "AsrStream"], nodes: 5 },
            { name: "讯飞语音评测", filename: "speech-eval.json", category: "speech", commands: ["StreamToWav", "XunFeiSpeechEval", "ConvertEvalResult"], nodes: 7 },
            
            // 其他服务
            { name: "添加好友", filename: "add-friend.json", category: "other", commands: ["DeviceIntentionVoice", "AsrStream", "AddFriend"], nodes: 12 },
            { name: "听故事", filename: "story.json", category: "other", commands: ["GetUserContext", "AiRoleChat", "AsrStream", "FySkDefault", "Condition"], nodes: 16 },
            { name: "发消息流程", filename: "send-message.json", category: "other", commands: ["DeviceIntentionVoice", "PreSendMessage", "Condition", "SendMessage"], nodes: 14 },
            { name: "敏感词检测", filename: "sensitive-check.json", category: "other", commands: ["JsonModel", "SensitiveWordCheck"], nodes: 4 },
            { name: "百度音频生成", filename: "baidu-audio-generate.json", category: "other", commands: ["AssembleBaiduParam", "BaiduAudio"], nodes: 6 },
            { name: "Volcengine克隆音色", filename: "volcengine-clone.json", category: "other", commands: ["BodyToJson", "CloneVoice", "TencentPostObject", "CloneVoiceSave"], nodes: 7 }
            // 这里可以添加更多服务...由于数量很多，实际应用中应该从后端API获取
        ];

        let filteredServices = [...servicesData];
        let currentCategory = 'all';

        // 初始化页面
        function init() {
            setTimeout(() => {
                document.getElementById('loading').style.display = 'none';
                document.getElementById('servicesContainer').style.display = 'block';
                renderServices();
                
                // 更新总数
                document.getElementById('totalServices').textContent = servicesData.length;
            }, 1000);

            // 绑定事件
            document.getElementById('searchInput').addEventListener('input', handleSearch);
            document.querySelectorAll('.tab').forEach(tab => {
                tab.addEventListener('click', handleCategoryFilter);
            });
        }

        // 处理搜索
        function handleSearch(e) {
            const query = e.target.value.toLowerCase().trim();
            filterServices(query, currentCategory);
        }

        // 处理分类筛选
        function handleCategoryFilter(e) {
            // 更新活动标签
            document.querySelectorAll('.tab').forEach(tab => tab.classList.remove('active'));
            e.target.classList.add('active');
            
            currentCategory = e.target.dataset.category;
            const query = document.getElementById('searchInput').value.toLowerCase().trim();
            filterServices(query, currentCategory);
        }

        // 筛选服务
        function filterServices(query, category) {
            filteredServices = servicesData.filter(service => {
                const matchesSearch = !query || 
                    service.name.toLowerCase().includes(query) ||
                    service.filename.toLowerCase().includes(query) ||
                    service.commands.some(cmd => cmd.toLowerCase().includes(query));
                
                const matchesCategory = category === 'all' || service.category === category;
                
                return matchesSearch && matchesCategory;
            });

            renderServices();
        }

        // 渲染服务
        function renderServices() {
            const container = document.getElementById('servicesContainer');
            const emptyState = document.getElementById('emptyState');

            if (filteredServices.length === 0) {
                container.innerHTML = '';
                emptyState.classList.remove('hidden');
                return;
            }

            emptyState.classList.add('hidden');

            // 按分类组织服务
            const servicesByCategory = {};
            filteredServices.forEach(service => {
                if (!servicesByCategory[service.category]) {
                    servicesByCategory[service.category] = [];
                }
                servicesByCategory[service.category].push(service);
            });

            // 分类显示名称映射
            const categoryNames = {
                'audio': '🎵 音频服务',
                'alarm': '⏰ 闹钟服务', 
                'voice': '🎤 语音服务',
                'text': '📝 文本服务',
                'speech': '🗣️ 语音识别',
                'other': '🔧 其他服务'
            };

            let html = '';
            Object.keys(servicesByCategory).sort().forEach(category => {
                const services = servicesByCategory[category];
                html += `
                    <div class="category-section">
                        <div class="category-header">
                            <h2>${categoryNames[category] || category}</h2>
                            <span class="category-count">${services.length}</span>
                        </div>
                        <div class="services-grid">
                            ${services.map(service => createServiceCard(service)).join('')}
                        </div>
                    </div>
                `;
            });

            container.innerHTML = html;
        }

        // 创建服务卡片
        function createServiceCard(service) {
            return `
                <div class="service-card" onclick="openEditor('${service.filename}', '${service.name}')">
                    <div class="service-actions">
                        <button class="action-btn" onclick="event.stopPropagation(); editService('${service.filename}')" title="编辑">
                            ✏️
                        </button>
                        <button class="action-btn" onclick="event.stopPropagation(); viewService('${service.filename}')" title="查看">
                            👁️
                        </button>
                    </div>
                    
                    <div class="service-header">
                        <div>
                            <div class="service-title">${service.name}</div>
                            <div class="service-filename">${service.filename}</div>
                        </div>
                    </div>
                    
                    <div class="service-stats">
                        <div class="service-stat">
                            <span>🔧</span>
                            <span>${service.nodes} 个节点</span>
                        </div>
                        <div class="service-stat">
                            <span>⚡</span>
                            <span>${service.commands.length} 个命令</span>
                        </div>
                    </div>
                    
                    <div class="service-commands">
                        ${service.commands.slice(0, 4).map(cmd => 
                            `<span class="command-tag">${cmd}</span>`
                        ).join('')}
                        ${service.commands.length > 4 ? 
                            `<span class="command-tag">+${service.commands.length - 4} 更多</span>` : ''
                        }
                    </div>
                </div>
            `;
        }

        // 打开编辑器
        function openEditor(filename, name) {
            document.getElementById('modalTitle').textContent = `编辑工作流: ${name}`;
            document.getElementById('workflowEditor').src = `index.html?service=${filename}`;
            document.getElementById('editorModal').style.display = 'block';
        }

        // 关闭编辑器
        function closeEditor() {
            document.getElementById('editorModal').style.display = 'none';
        }

        // 编辑服务
        function editService(filename) {
            // 这里可以实现直接编辑JSON配置的功能
            alert(`编辑服务配置: ${filename}`);
        }

        // 查看服务
        function viewService(filename) {
            // 这里可以实现查看服务详情的功能
            window.open(`services/${filename}`, '_blank');
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('editorModal');
            if (event.target == modal) {
                closeEditor();
            }
        }

        // ESC键关闭模态框
        document.addEventListener('keydown', function(event) {
            if (event.key === 'Escape') {
                closeEditor();
            }
        });

        // 初始化页面
        init();
    </script>
</body>
</html>
